/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.close.visitor.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.close.common.constant.VisitorDicConstant;
import com.hopes.bizz.close.visitor.dto.ClosedVisitorApplyAuditDTO;
import com.hopes.bizz.close.visitor.dto.ClosedVisitorApplyDTO;
import com.hopes.bizz.close.visitor.dto.ClosedVisitorApplyExcelDTO;
import com.hopes.bizz.close.visitor.entity.ClosedVisitorApplyEntity;
import com.hopes.bizz.close.visitor.service.ClosedVisitorApplyService;
import com.hopes.bizz.close.visitor.vo.ClosedVisitorApplyExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 访客进出申请
 *
 * @author pig
 * @date 2023-10-25 13:26:19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/apply" )
@Tag(description = "apply" , name = "访客进出申请管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ClosedVisitorApplyController {

    private final ClosedVisitorApplyService closedVisitorApplyService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param closedVisitorApplyDTO 访客进出申请
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('visitor_apply_view')" )
    public R getClosedVisitorApplyPage(Page page, ClosedVisitorApplyDTO closedVisitorApplyDTO) {
        LambdaQueryWrapper<ClosedVisitorApplyEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(closedVisitorApplyDTO.getOrgId()), ClosedVisitorApplyEntity::getOrgId, closedVisitorApplyDTO.getOrgId());
        wrapper.eq(ObjUtil.isNotNull(closedVisitorApplyDTO.getAuditStatus()), ClosedVisitorApplyEntity::getAuditStatus, closedVisitorApplyDTO.getAuditStatus());
        wrapper.eq(ObjUtil.isNotNull(closedVisitorApplyDTO.getVisitPurpose()), ClosedVisitorApplyEntity::getVisitPurpose, closedVisitorApplyDTO.getVisitPurpose());
        wrapper.gt(ObjUtil.isNotNull(closedVisitorApplyDTO.getStartTime()), ClosedVisitorApplyEntity::getEffectiveTime, closedVisitorApplyDTO.getStartTime());
        wrapper.lt(ObjUtil.isNotNull(closedVisitorApplyDTO.getEndTime()), ClosedVisitorApplyEntity::getEffectiveTime, closedVisitorApplyDTO.getEndTime());
        wrapper.like(StrUtil.isNotBlank(closedVisitorApplyDTO.getKeyword()), ClosedVisitorApplyEntity::getName, closedVisitorApplyDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedVisitorApplyDTO.getKeyword()), ClosedVisitorApplyEntity::getPhone, closedVisitorApplyDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedVisitorApplyDTO.getKeyword()), ClosedVisitorApplyEntity::getIdNum, closedVisitorApplyDTO.getKeyword());
        return R.ok(closedVisitorApplyService.page(page, wrapper));
    }

    /**
     * 分页查询 （移动端）我的访客申请
     * @param page 分页对象
     * @param closedVisitorApplyDTO 访客进出申请
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/selfPage" )
    @PreAuthorize("@pms.hasPermission('visitor_apply_view')" )
    public R getAppVisitorApplySelfPage(Page page, ClosedVisitorApplyDTO closedVisitorApplyDTO) {
        return R.ok(closedVisitorApplyService.queryAppVisitorApplyPage(page, closedVisitorApplyDTO));
    }


    /**
     * 分页查询 （移动端）企业待审批的申请
     * @param page 分页对象
     * @param closedVisitorApplyDTO 访客进出申请
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/audit/toDoPage" )
    @PreAuthorize("@pms.hasPermission('visitor_apply_view')" )
    public R getAppVisitorAuditToDoPage(Page page, ClosedVisitorApplyDTO closedVisitorApplyDTO) {
        final List<Integer> auditStatusList = Collections.singletonList(VisitorDicConstant.CLOSE_VISITOR_AUDIT_STATUS_NOT_AUDIT);
        return R.ok(closedVisitorApplyService.queryAppVisitorAuditPage(page, auditStatusList, closedVisitorApplyDTO));
    }

    /**
     * 分页查询 （移动端）企业审批完成的申请
     * @param page 分页对象
     * @param closedVisitorApplyDTO 访客进出申请
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/audit/donePage" )
    @PreAuthorize("@pms.hasPermission('visitor_apply_view')" )
    public R getAppVisitorAuditDonePage(Page page, ClosedVisitorApplyDTO closedVisitorApplyDTO) {
        final List<Integer> auditStatusList = Arrays.asList(
                VisitorDicConstant.CLOSE_VISITOR_AUDIT_STATUS_PASS_AUDIT,
                VisitorDicConstant.CLOSE_VISITOR_AUDIT_STATUS_NOT_PASS_AUDIT);
        return R.ok(closedVisitorApplyService.queryAppVisitorAuditPage(page, auditStatusList, closedVisitorApplyDTO));
    }


    /**
     * 通过id查询访客进出申请
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('visitor_apply_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(closedVisitorApplyService.getById(id));
    }

    /**
     * 新增访客进出申请
     * @param closedVisitorApply 访客进出申请
     * @return R
     */
    @Operation(summary = "新增访客进出申请" , description = "新增访客进出申请" )
    @SysLog("新增访客进出申请" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('visitor_apply_add')" )
    public R save(@RequestBody ClosedVisitorApplyEntity closedVisitorApply) {
        return R.ok(closedVisitorApplyService.save(closedVisitorApply));
    }


    /**
     * 新增并提交访客进出申请
     * @param closedVisitorApply 访客进出申请
     * @return R
     */
    @Operation(summary = "新增并提交访客进出申请" , description = "新增并提交访客进出申请" )
    @SysLog("新增并提交访客进出申请" )
    @PostMapping("/saveAndSubmit")
    @PreAuthorize("@pms.hasPermission('visitor_apply_add')" )
    public R saveAndSubmit(@RequestBody ClosedVisitorApplyEntity closedVisitorApply) {
        return R.ok(closedVisitorApplyService.saveAndSubmit(closedVisitorApply));
    }


    /**
     * 修改访客进出申请
     * @param closedVisitorApply 访客进出申请
     * @return R
     */
    @Operation(summary = "修改访客进出申请" , description = "修改访客进出申请" )
    @SysLog("修改访客进出申请" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('visitor_apply_edit')" )
    public R updateById(@RequestBody ClosedVisitorApplyEntity closedVisitorApply) {
        return R.ok(closedVisitorApplyService.updateById(closedVisitorApply));
    }


    /**
     * 提交入园申请
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "提交入园申请", description = "提交入园申请")
    @SysLog("提交入园申请")
    @GetMapping("/submit/{id}")
    @PreAuthorize("@pms.hasPermission('visitor_apply_edit')")
    public R submitById(@PathVariable("id") Long id) {
        return R.ok(closedVisitorApplyService.submitById(id));
    }

    /**
     * 审核访客进出申请
     * @param closedVisitorApplyAuditDTO 审核访客进出申请
     * @return R
     */
    @Operation(summary = "审核访客进出申请" , description = "审核访客进出申请" )
    @SysLog("审核访客进出申请" )
    @PutMapping("/audit")
    @PreAuthorize("@pms.hasPermission('visitor_apply_audit')" )
    public R auditById(@RequestBody ClosedVisitorApplyAuditDTO closedVisitorApplyAuditDTO) {
        return R.ok(closedVisitorApplyService.auditById(closedVisitorApplyAuditDTO));
    }

    /**
     * 通过id删除访客进出申请
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除访客进出申请" , description = "通过id删除访客进出申请" )
    @SysLog("通过id删除访客进出申请" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('visitor_apply_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(closedVisitorApplyService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('visitor_apply_export')")
    public List<ClosedVisitorApplyExcelVO> template() {
        return closedVisitorApplyService.exportExcel(Collections.singletonList(new ClosedVisitorApplyEntity()));
    }

    /**
     * 导出excel 表格
     * @param closedVisitorApplyDTO 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('visitor_apply_export')")
    public List<ClosedVisitorApplyExcelVO> export( ClosedVisitorApplyDTO closedVisitorApplyDTO) {
        final LambdaQueryWrapper<ClosedVisitorApplyEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(closedVisitorApplyDTO.getOrgId()), ClosedVisitorApplyEntity::getOrgId, closedVisitorApplyDTO.getOrgId());
        wrapper.eq(ObjUtil.isNotNull(closedVisitorApplyDTO.getAuditStatus()), ClosedVisitorApplyEntity::getAuditStatus, closedVisitorApplyDTO.getAuditStatus());
        wrapper.eq(ObjUtil.isNotNull(closedVisitorApplyDTO.getVisitPurpose()), ClosedVisitorApplyEntity::getVisitPurpose, closedVisitorApplyDTO.getVisitPurpose());
        wrapper.gt(ObjUtil.isNotNull(closedVisitorApplyDTO.getStartTime()), ClosedVisitorApplyEntity::getEffectiveTime, closedVisitorApplyDTO.getStartTime());
        wrapper.lt(ObjUtil.isNotNull(closedVisitorApplyDTO.getEndTime()), ClosedVisitorApplyEntity::getInvalidTime, closedVisitorApplyDTO.getEndTime());
        wrapper.like(StrUtil.isNotBlank(closedVisitorApplyDTO.getKeyword()), ClosedVisitorApplyEntity::getName, closedVisitorApplyDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedVisitorApplyDTO.getKeyword()), ClosedVisitorApplyEntity::getPhone, closedVisitorApplyDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedVisitorApplyDTO.getKeyword()), ClosedVisitorApplyEntity::getIdNum, closedVisitorApplyDTO.getKeyword());
        return closedVisitorApplyService.exportExcel(closedVisitorApplyService.list(wrapper));
    }

    /**
     * 导入excel 表格
     * @param closedVisitorApplyExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('visitor_apply_export')")
    public R importExcel(@RequestExcel List<ClosedVisitorApplyExcelDTO> closedVisitorApplyExcelDtoList, BindingResult bindingResult) {
        return closedVisitorApplyService.importExcel(closedVisitorApplyExcelDtoList, bindingResult);
    }
}
